/** layui-v0.0.7 跨设备模块化前端框架@LGPL www.layui.com By 贤心 */
;
layui.define(["jquery", "layer", "laytpl", "upload"],
function(i) {
    var a = "2.0.83",
    e = layui.jquery,
    t = layui.layer,
    l = layui.laytpl,
    n = "layim-show",
    s = "layim-this",
    o = {},
    c = function() {
        this.v = a,
        e("body").on("click", "*[layim-event]",
        function(i) {
            var a = e(this),
            t = a.attr("layim-event");
            F[t] ? F[t].call(this, a, i) : ""
        })
    };
    c.prototype.config = function(i) {
        var a = [layui.cache.dir + "css/pc/layim/skin/01.jpg", layui.cache.dir + "css/pc/layim/skin/02.jpg", layui.cache.dir + "css/pc/layim/skin/03.jpg", layui.cache.dir + "css/pc/layim/skin/04.jpg"];
        return i = i || {},
        i.skin = i.skin || [],
        layui.each(i.skin,
        function(i, e) {
            a.unshift(e)
        }),
        i.skin = a,
        i = e.extend({
            isfriend: !0,
            isgroup: !0
        },
        i),
        window.JSON && window.JSON.parse ? (w(i), this) : void 0
    },
    c.prototype.on = function(i, a) {
        return "function" == typeof a && (o[i] ? o[i].push(a) : o[i] = [a]),
        this
    },
    c.prototype.cache = function() {
        return k
    },
    c.prototype.chat = function(i) {
        return window.JSON && window.JSON.parse ? (T(i), this) : void 0
    },
    c.prototype.setChatMin = function() {
        return L(),
        this
    },
    c.prototype.getMessage = function(i) {
        return N(i),
        this
    },
    c.prototype.addList = function(i) {
        return D(i),
        this
    },
    c.prototype.removeList = function(i) {
        return $(i),
        this
    },
    c.prototype.content = function(i) {
        return layui.laytpl.content(i)
    };
    var r = function(i) {
        var a = {
            friend: "该分组下暂无好友",
            group: "暂无群组",
            history: "暂无历史会话"
        };
        return i = i || {},
        i.item = i.item || "d." + i.type,
        ["{{# var length = 0; layui.each(" + i.item + ", function(i, data){ length++; }}", '<li layim-event="chat" data-type="' + i.type + '" data-index="{{ ' + (i.index || "i") + ' }}" id="layim-' + i.type + '{{ data.id }}"><img src="{{ data.avatar }}"><span>{{ data.username||data.groupname||data.name||"佚名" }}</span><p>{{ data.remark||data.sign||"" }}</p></li>', "{{# }); if(length === 0){ }}", '<li class="layim-null">' + (a[i.type] || "暂无数据") + "</li>", "{{# } }}"].join("")
    },
    d = ['<div class="layui-layim-main">', '<div class="layui-layim-info">', '<div class="layui-layim-user">{{ d.mine.username }}</div>', '<div class="layui-layim-status">', '{{# if(d.mine.status === "online"){ }}', '<span class="layui-icon layim-status-online" layim-event="status" lay-type="show">&#xe617;</span>', '{{# } else if(d.mine.status === "hide") { }}', '<span class="layui-icon layim-status-hide" layim-event="status" lay-type="show">&#xe60f;</span>', "{{# } }}", '<ul class="layui-anim layim-menu-box">', '<li {{d.mine.status === "online" ? "class=layim-this" : ""}} layim-event="status" lay-type="online"><i class="layui-icon">&#xe618;</i><cite class="layui-icon layim-status-online">&#xe617;</cite>在线</li>', '<li {{d.mine.status === "hide" ? "class=layim-this" : ""}} layim-event="status" lay-type="hide"><i class="layui-icon">&#xe618;</i><cite class="layui-icon layim-status-hide">&#xe60f;</cite>隐身</li>', "</ul>", "</div>", '<p class="layui-layim-remark" title="{{# if(d.mine.sign){ }}{{d.mine.sign}}{{# } }}">{{ d.mine.remark||d.mine.sign||"你很懒，没有写签名" }}</p>', "</div>", '<ul class="layui-layim-tab{{# if(!d.base.isfriend || !d.base.isgroup){ }}', " layim-tab-two", '{{# } }}">', '<li class="layui-icon', "{{# if(!d.base.isfriend){ }}", " layim-hide", "{{# } else { }}", " layim-this", "{{# } }}", '" title="联系人" layim-event="tab" lay-type="friend">&#xe612;</li>', '<li class="layui-icon', "{{# if(!d.base.isgroup){ }}", " layim-hide", "{{# } else if(!d.base.isfriend) { }}", " layim-this", "{{# } }}", '" title="群组" layim-event="tab" lay-type="group">&#xe613;</li>', '<li class="layui-icon" title="历史会话" layim-event="tab" lay-type="history">&#xe611;</li>', "</ul>", '<ul class="layim-tab-content {{# if(d.base.isfriend){ }}layim-show{{# } }} layim-list-friend">', '{{# layui.each(d.friend, function(index, item){ var spread = d.local["spread"+index]; }}', "<li>", '<h5 layim-event="spread" lay-type="{{ spread }}"><i class="layui-icon">{{# if(spread === "true"){ }}&#xe61a;{{# } else {  }}&#xe602;{{# } }}</i><span>{{ item.groupname||"未命名分组"+index }}</span><em>(<cite class="layim-count"> {{ (item.list||[]).length }}</cite>)</em></h5>', '<ul class="layui-layim-list {{# if(spread === "true"){ }}', " layim-show", '{{# } }}">', r({
        type: "friend",
        item: "item.list",
        index: "index"
    }), "</ul>", "</li>", "{{# }); if(d.friend.length === 0){ }}", '<li><ul class="layui-layim-list layim-show"><li class="layim-null">暂无联系人</li></ul>', "{{# } }}", "</ul>", '<ul class="layim-tab-content {{# if(!d.base.isfriend && d.base.isgroup){ }}layim-show{{# } }}">', "<li>", '<ul class="layui-layim-list layim-show layim-list-group">', r({
        type: "group"
    }), "</ul>", "</li>", "</ul>", '<ul class="layim-tab-content  {{# if(!d.base.isfriend && !d.base.isgroup){ }}layim-show{{# } }}">', "<li>", '<ul class="layui-layim-list layim-show layim-list-history">', r({
        type: "history"
    }), "</ul>", "</li>", "</ul>", '<ul class="layim-tab-content">', "<li>", '<ul class="layui-layim-list layim-show" id="layui-layim-search"></ul>', "</li>", "</ul>", '<ul class="layui-layim-tool">', '<li class="layui-icon layim-tool-search" layim-event="search" title="搜索">&#xe615;</li>', '<li class="layui-icon layim-tool-skin" layim-event="skin" title="换肤">&#xe61b;</li>', "{{# if(d.base.find){ }}", '<li class="layui-icon layim-tool-find" layim-event="find" title="查找">&#xe61f;</li>', "{{# } }}", "{{# if(!d.base.copyright){ }}", '<li class="layui-icon layim-tool-about" layim-event="about" title="关于">&#xe60b;</li>', "{{# } }}", "</ul>", '<div class="layui-layim-search"><input><label class="layui-icon" layim-event="closeSearch">&#x1007;</label></div>', "</div>"].join(""),
    u = ['<ul class="layui-layim-skin">', "{{# layui.each(d.skin, function(index, item){ }}", '<li><img layim-event="setSkin" src="{{ item }}"></li>', "{{# }); }}", '<li layim-event="setSkin"><cite>默认</cite></li>', "</ul>"].join(""),
    y = ['<div class="layim-chat layim-chat-{{d.data.type}}">', '<div class="layim-chat-title">', '<a class="layim-chat-other">', '<img src="{{ d.data.avatar }}"><span layim-event="{{ d.data.type==="group" ? "groupMembers" : "" }}">{{ d.data.name||"佚名" }} {{# if(d.data.type==="group"){ }} <em class="layim-chat-members"></em><i class="layui-icon">&#xe61a;</i> {{# } }}</span>', "</a>", "</div>", '<div class="layim-chat-main">', "<ul></ul>", "</div>", '<div class="layim-chat-footer">', '<div class="layim-chat-tool" data-json="{{encodeURIComponent(JSON.stringify(d.data))}}">', '<span class="layui-icon layim-tool-face" title="选择表情" layim-event="face">&#xe60c;</span>', '<span class="layui-icon layim-tool-image" title="上传图片" layim-event="image">&#xe60d;<input type="file" name="file"></span>', "{{# if(d.base && d.base.uploadFile){ }}", '<span class="layui-icon layim-tool-image" title="发送文件" layim-event="image" data-type="file">&#xe61d;<input type="file" name="file"></span>', "{{# }; }}", "{{# if(d.base && d.base.chatLog){ }}", '<span class="layim-tool-log" layim-event="chatLog"><i class="layui-icon">&#xe60e;</i>聊天记录</span>', "{{# }; }}", "</div>", '<div class="layim-chat-textarea"><textarea></textarea></div>', '<div class="layim-chat-bottom">', '<div class="layim-chat-send">', "{{# if(!d.base.brief){ }}", '<span class="layim-send-close" layim-event="closeThisChat">关闭</span>', "{{# } }}", '<span class="layim-send-btn" layim-event="send">发送</span>', '<span class="layim-send-set" layim-event="setSend" lay-type="show"><em class="layui-edge"></em></span>', '<ul class="layui-anim layim-menu-box">', '<li {{d.local.sendHotKey !== "Ctrl+Enter" ? "class=layim-this" : ""}} layim-event="setSend" lay-type="Enter"><i class="layui-icon">&#xe618;</i>按Enter键发送消息</li>', '<li {{d.local.sendHotKey === "Ctrl+Enter" ? "class=layim-this" : ""}} layim-event="setSend"  lay-type="Ctrl+Enter"><i class="layui-icon">&#xe618;</i>按Ctrl+Enter键发送消息</li>', "</ul>", "</div>", "</div>", "</div>", "</div>"].join("");
    layui.laytpl.date = function(i) {
        return new Date(i || new Date).toLocaleString().replace(/\//g, "-").replace(/(\s)\D*(\d)/g, "$1$2")
    },
    layui.laytpl.content = function(i) {
        var a = function(i) {
            return new RegExp("\\n*\\[" + (i || "") + "(pre|div|p|table|thead|th|tbody|tr|td|ul|li|ol|li|dl|dt|dd|h2|h3|h4|h5)([\\s\\S]*?)\\]\\n*", "g")
        };
        return i = (i || "").replace(/&(?!#?[a-zA-Z0-9]+;)/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/'/g, "&#39;").replace(/"/g, "&quot;").replace(/@(\S+)(\s+?|$)/g, '@<a href="javascript:;">$1</a>$2').replace(/\s{2}/g, "&nbsp").replace(/img\[([^\s]+?)\]/g,
        function(i) {
            return '<img class="layui-layim-photos" src="' + i.replace(/(^img\[)|(\]$)/g, "") + '">'
        }).replace(/file\([\s\S]+?\)\[[\s\S]*?\]/g,
        function(i) {
            var a = (i.match(/file\(([\s\S]+?)\)\[/) || [])[1],
            e = (i.match(/\)\[([\s\S]*?)\]/) || [])[1];
            return a ? '<a class="layui-layim-file" href="' + a + '" target="_blank"><i class="layui-icon">&#xe61e;</i><cite>' + (e || a) + "</cite></a>": i
        }).replace(/face\[([^\s\[\]]+?)\]/g,
        function(i) {
            var a = i.replace(/^face/g, "");
            return '<img alt="' + a + '" title="' + a + '" src="' + _[a] + '">'
        }).replace(/a\([\s\S]+?\)\[[\s\S]*?\]/g,
        function(i) {
            var a = (i.match(/a\(([\s\S]+?)\)\[/) || [])[1],
            e = (i.match(/\)\[([\s\S]*?)\]/) || [])[1];
            return a ? '<a href="' + a + '" target="_blank">' + (e || a) + "</a>": i
        }).replace(a(), "<$1 $2>").replace(a("/"), "</$1>").replace(/\n/g, "<br>")
    };
    var m, f, p, h, v, g = ['<li {{ d.mine ? "class=layim-chat-mine" : "" }}>', '<div class="layim-chat-user"><img src="{{ d.avatar }}"><cite>', "{{# if(d.mine){ }}", '<i>{{ layui.laytpl.date(d.timestamp) }}</i>{{ d.username||"佚名" }}', "{{# } else { }}", '{{ d.username||"佚名" }}<i>{{ layui.laytpl.date(d.timestamp) }}</i>', "{{# } }}", "</cite></div>", '<div class="layim-chat-text">{{ layui.laytpl.content(d.content||"&nbsp") }}</div>', "</li>"].join(""),
    b = '<li class="layim-chatlist-{{ d.data.type }}{{ d.data.id }} layim-this" layim-event="tabChat"><img src="{{ d.data.avatar }}"><span>{{ d.data.name||"佚名" }}</span>{{# if(!d.base.brief){ }}<i class="layui-icon" layim-event="closeChat">&#x1007;</i>{{# } }}</li>',
    x = function(i, a, l) {
        return i = i || {},
        e.ajax({
            url: i.url,
            type: i.type || "get",
            data: i.data,
            dataType: i.dataType || "json",
            cache: !1,
            success: function(i) {
                0 == i.code ? a && a(i.data || {}) : t.msg(i.msg || (l || "Error") + ": LAYIM_NOT_GET_DATA", {
                    time: 5e3
                })
            },
            error: function(i, a) {
                window.console && console.log && console.error("LAYIM_DATE_ERROR：" + a)
            }
        })
    },
    k = {
        message: {},
        chat: []
    },
    w = function(i) {
        var a = i.mine || {},
        t = layui.data("layim")[a.id] || {},
        n = {
            base: i,
            local: t,
            mine: a,
            history: t.history || {}
        };
        return k = e.extend(k, n),
        i.brief ? layui.each(o.ready,
        function(i, a) {
            a && a(n)
        }) : void x(i.init,
        function(a) {
            var t = i.mine || a.mine || {},
            n = layui.data("layim")[t.id] || {},
            s = {
                base: i,
                local: n,
                mine: t,
                friend: a.friend || [],
                group: a.group || [],
                history: n.history || {}
            };
            k = e.extend(k, s),
            C(l(d).render(s)),
            n.close && j(),
            layui.each(o.ready,
            function(i, a) {
                a && a(s)
            })
        },
        "INIT")
    },
    C = function(i) {
        return t.open({
            type: 1,
            area: ["260px", "520px"],
            skin: "layui-box layui-layim",
            title: "&#8203;",
            offset: "rb",
            id: "layui-layim",
            shade: !1,
            moveType: 1,
            shift: 2,
            content: i,
            success: function(i) {
                var a = layui.data("layim")[k.mine.id] || {},
                l = a.skin;
                m = i,
                m.css({
                    "background-image": l ? "url(" + l + ")": "none"
                }),
                k.base.right && i.css("margin-left", "-" + k.base.right),
                f && t.close(f.attr("times"));
                var n = [],
                s = i.find(".layim-list-history");
                s.find("li").each(function() {
                    n.push(e(this).prop("outerHTML"))
                }),
                n.length > 0 && (n.reverse(), s.html(n.join(""))),
                S()
            },
            cancel: function(i) {
                j();
                var a = layui.data("layim")[k.mine.id] || {};
                return a.close = !0,
                layui.data("layim", {
                    key: k.mine.id,
                    value: a
                }),
                !1
            }
        })
    },
    S = function() {
        m.on("contextmenu",
        function(i) {
            return i.cancelBubble = !0,
            i.returnValue = !1,
            !1
        });
        var i = function() {
            t.closeAll("tips")
        };
        m.find(".layim-list-history").on("contextmenu", "li",
        function(a) {
            var l = e(this),
            n = '<ul data-id="' + l[0].id + '" data-index="' + l.data("index") + '"><li layim-event="menuHistory" data-type="one">移除该会话</li><li layim-event="menuHistory" data-type="all">清空全部会话列表</li></ul>';
            l.hasClass("layim-null") || (t.tips(n, this, {
                tips: 1,
                time: 0,
                shift: 5,
                fix: !0,
                skin: "layui-box layui-layim-contextmenu",
                success: function(i) {
                    var a = function(i) {
                        K(i)
                    };
                    i.off("mousedown", a).on("mousedown", a)
                }
            }), e(document).off("mousedown", i).on("mousedown", i), e(window).off("resize", i).on("resize", i))
        })
    },
    j = function(i) {
        return f && t.close(f.attr("times")),
        m && m.hide(),
        k.mine = k.mine || {},
        t.open({
            type: 1,
            title: !1,
            id: "layui-layim-close",
            skin: "layui-box layui-layim-min layui-layim-close",
            shade: !1,
            closeBtn: !1,
            shift: 2,
            offset: "rb",
            content: '<img src="' + (k.mine.avatar || layui.cache.dir + "css/pc/layim/skin/logo.jpg") + '"><span>' + (i || k.base.title || "我的LayIM") + "</span>",
            success: function(i, a) {
                f = i,
                k.base.right && i.css("margin-left", "-" + k.base.right),
                i.on("click",
                function() {
                    t.close(a),
                    m.show();
                    var i = layui.data("layim")[k.mine.id] || {};
                    delete i.close,
                    layui.data("layim", {
                        key: k.mine.id,
                        value: i
                    })
                })
            }
        })
    },
    T = function(i) {
        i = i || {};
        var a = e("#layui-layim-chat"),
        n = {
            data: i,
            base: k.base,
            local: k.local
        };
        if (!i.id) return t.msg("非法用户");
        if (a[0]) {
            var s = p.find(".layim-chat-list"),
            c = s.find(".layim-chatlist-" + i.type + i.id);
            return "none" === p.css("display") && p.show(),
            h && t.close(h.attr("times")),
            1 !== s.find("li").length || c[0] || (p.css("width", "800px"), s.css("display", "inline-block")),
            c[0] || (s.append(l(b).render(n)), a.append(l(y).render(n))),
            I(s.find(".layim-chatlist-" + i.type + i.id)),
            c[0] || A(),
            O(i),
            J(),
            v
        }
        var r = v = t.open({
            type: 1,
            area: ["600px", "520px"],
            skin: "layui-box layui-layim-chat",
            id: "layui-layim-chat",
            title: "&#8203;",
            shade: !1,
            moveType: 1,
            maxmin: !0,
            closeBtn: k.base.brief ? !1 : 1,
            content: l('<ul class="layim-chat-list">' + b + "</ul>" + y).render(n),
            success: function(a) {
                var e = layui.data("layim")[k.mine.id] || {},
                l = e.skin;
                p = a,
                p.css({
                    "background-image": l ? "url(" + l + ")": "none"
                }),
                J(),
                O(i),
                layui.each(o.chatChange,
                function(i, a) {
                    a && a(E())
                }),
                A(),
                q(),
                a.on("click", ".layui-layim-photos",
                function() {
                    var i = this.src;
                    t.close(T.photosIndex),
                    t.photos({
                        photos: {
                            data: [{
                                alt: "大图模式",
                                src: i
                            }]
                        },
                        shade: .01,
                        closeBtn: 2,
                        shift: 0,
                        success: function(i, a) {
                            T.photosIndex = a
                        }
                    })
                })
            },
            min: function() {
                return L(),
                !1
            },
            end: function() {
                t.closeAll("tips")
            }
        });
        return r
    },
    L = function(i) {
        var a = i || E().data;
        p && !i && p.hide(),
        h && t.close(h.attr("times")),
        t.open({
            type: 1,
            title: !1,
            id: "layui-layim-min",
            skin: "layui-box layui-layim-min",
            shade: !1,
            closeBtn: !1,
            shift: a.shift || 2,
            offset: e(window).height() - 50,
            content: '<img src="' + a.avatar + '"><span>' + a.name + "</span>",
            success: function(a, e) {
                i || (h = a),
                a.on("click",
                function() {
                    t.close(e),
                    i ? layui.each(k.chat,
                    function(i, a) {
                        T(a)
                    }) : p.show(),
                    i && (k.chat = [], z())
                })
            }
        })
    },
    I = function(i, a) {
        i = i || e(".layim-chat-list ." + s);
        var l = -1 === i.index() ? 0 : i.index(),
        n = ".layim-chat",
        c = p.find(n).eq(l);
        if (a) {
            i.hasClass(s) && I(0 === l ? i.next() : i.prev()),
            i.remove(),
            c.remove();
            var r = p.find(n).length;
            return 1 === r && (p.find(".layim-chat-list").hide(), p.css("width", "600px")),
            0 === r && t.close(v),
            !1
        }
        i.addClass(s).siblings().removeClass(s),
        c.css("display", "inline-block").siblings(n).hide(),
        c.find("textarea").focus(),
        layui.each(o.chatChange,
        function(i, a) {
            a && a(E())
        }),
        q()
    },
    q = function() {
        var i = E(),
        a = k.message[i.data.type + i.data.id];
        a && delete k.message[i.data.type + i.data.id]
    },
    E = function() {
        var i = e(".layim-chat-list ." + s).index(),
        a = p.find(".layim-chat").eq(i),
        t = JSON.parse(decodeURIComponent(a.find(".layim-chat-tool").data("json")));
        return {
            elem: a,
            data: t,
            textarea: a.find("textarea")
        }
    },
    O = function(i) {
        var a = layui.data("layim")[k.mine.id] || {},
        e = {},
        t = a.history || {},
        n = t[i.type + i.id];
        if (m) {
            var s = m.find(".layim-list-history");
            if (i.historyTime = (new Date).getTime(), t[i.type + i.id] = i, a.history = t, layui.data("layim", {
                key: k.mine.id,
                value: a
            }), !n) {
                e[i.type + i.id] = i;
                var o = l(r({
                    type: "history",
                    item: "d.data"
                })).render({
                    data: e
                });
                s.prepend(o),
                s.find(".layim-null").remove()
            }
        }
    },
    M = function() {
        var i = {
            username: k.mine ? k.mine.username: "访客",
            avatar: k.mine ? k.mine.avatar: layui.cache.dir + "css/pc/layim/skin/logo.jpg",
            id: k.mine ? k.mine.id: null,
            mine: !0
        },
        a = E(),
        e = a.elem.find(".layim-chat-main ul"),
        n = k.base.maxLength || 3e3;
        if (i.content = a.textarea.val(), "" !== i.content.replace(/\s/g, "")) {
            if (i.content.length > n) return t.msg("内容最长不能超过" + n + "个字符");
            e.append(l(g).render(i));
            var s = {
                mine: i,
                to: a.data
            },
            c = {
                username: s.mine.username,
                avatar: s.mine.avatar,
                id: s.to.id,
                type: s.to.type,
                content: s.mine.content,
                timestamp: (new Date).getTime(),
                mine: !0
            };
            H(c),
            layui.each(o.sendMessage,
            function(i, a) {
                a && a(s)
            })
        }
        z(),
        a.textarea.val("").focus()
    },
    N = function(i) {
        i = i || {};
        var a = e(".layim-chatlist-" + i.type + i.id),
        t = {},
        n = a.index();
        if (i.timestamp = i.timestamp || (new Date).getTime(), H(i), !p && i.content || -1 === n) {
            if (k.message[i.type + i.id]) k.message[i.type + i.id].push(i);
            else if (k.message[i.type + i.id] = [i], "friend" === i.type) {
                var s;
                layui.each(k.friend,
                function(a, e) {
                    return layui.each(e.list,
                    function(a, e) {
                        return e.id == i.id ? (e.type = "friend", e.name = e.username, k.chat.push(e), s = !0) : void 0
                    }),
                    s ? !0 : void 0
                }),
                s || (i.name = i.username + "<cite>临时会话<cite>", k.chat.push(i))
            } else if ("group" === i.type) {
                var o;
                layui.each(k.group,
                function(a, e) {
                    return e.id == i.id ? (e.type = "group", e.name = e.groupname, k.chat.push(e), o = !0) : void 0
                }),
                o || (i.name = i.groupname, k.chat.push(i))
            } else i.name = i.name || i.username || i.groupname,
            k.chat.push(i);
            return "group" === i.type && layui.each(k.group,
            function(a, e) {
                return e.id == i.id ? (t.avatar = e.avatar, !0) : void 0
            }),
            L({
                name: "收到新消息",
                avatar: t.avatar || i.avatar,
                shift: 6
            })
        }
        var c = E();
        c.data.type + c.data.id !== i.type + i.id && (a.addClass("layui-anim layer-anim-06"), setTimeout(function() {
            a.removeClass("layui-anim layer-anim-06")
        },
        300));
        var r = p.find(".layim-chat").eq(n),
        d = r.find(".layim-chat-main ul");
        "" !== i.content.replace(/\s/g, "") && d.append(l(g).render(i)),
        z()
    },
    H = function(i) {
        var a = layui.data("layim")[k.mine.id] || {},
        e = a.chatlog || {};
        e[i.type + i.id] ? (e[i.type + i.id].push(i), e[i.type + i.id].length > 50 && e[i.type + i.id].shift()) : e[i.type + i.id] = [i],
        a.chatlog = e,
        layui.data("layim", {
            key: k.mine.id,
            value: a
        })
    },
    A = function() {
        var i = layui.data("layim")[k.mine.id] || {},
        a = E(),
        e = i.chatlog || {},
        t = a.elem.find(".layim-chat-main ul");
        layui.each(e[a.data.type + a.data.id],
        function(i, a) {
            t.append(l(g).render(a))
        }),
        z()
    },
    D = function(i) {
        var a, e = m.find(".layim-list-" + i.type),
        n = {};
        if (k[i.type]) if ("friend" === i.type) layui.each(k.friend,
        function(e, l) {
            return i.groupid == l.id ? (layui.each(k.friend[e].list,
            function(e, t) {
                return t.id == i.id ? a = !0 : void 0
            }), a ? t.msg("好友 [" + (i.username || "") + "] 已经存在列表中", {
                shift: 6
            }) : (k.friend[e].list = k.friend[e].list || [], n[k.friend[e].list.length] = i, i.groupIndex = e, k.friend[e].list.push(i), !0)) : void 0
        });
        else if ("group" === i.type) {
            if (layui.each(k.group,
            function(e, t) {
                return t.id == i.id ? a = !0 : void 0
            }), a) return t.msg("您已是 [" + (i.groupname || "") + "] 的群成员", {
                shift: 6
            });
            n[k.group.length] = i,
            k.group.push(i)
        }
        if (!a) {
            var s = l(r({
                type: i.type,
                item: "d.data",
                index: "friend" === i.type ? "data.groupIndex": null
            })).render({
                data: n
            });
            if ("friend" === i.type) {
                var o = e.find(">li").eq(i.groupIndex);
                o.find(".layui-layim-list").append(s),
                o.find(".layim-count").html(k.friend[i.groupIndex].list.length),
                o.find(".layim-null")[0] && o.find(".layim-null").remove()
            } else "group" === i.type && (e.append(s), e.find(".layim-null")[0] && e.find(".layim-null").remove())
        }
    },
    $ = function(i) {
        var a = m.find(".layim-list-" + i.type);
        k[i.type] && ("friend" === i.type ? layui.each(k.friend,
        function(e, t) {
            layui.each(t.list,
            function(t, l) {
                if (i.id == l.id) {
                    var n = a.find(">li").eq(e);
                    n.find(".layui-layim-list>li");
                    return n.find(".layui-layim-list>li").eq(t).remove(),
                    k.friend[e].list.splice(t, 1),
                    n.find(".layim-count").html(k.friend[e].list.length),
                    0 === k.friend[e].list.length && n.find(".layui-layim-list").html('<li class="layim-null">该分组下已无好友了</li>'),
                    !0
                }
            })
        }) : "group" === i.type && layui.each(k.group,
        function(e, t) {
            return i.id == t.id ? (a.find(">li").eq(e).remove(), k.group.splice(e, 1), 0 === k.group.length && a.html('<li class="layim-null">暂无群组</li>'), !0) : void 0
        }))
    },
    z = function() {
        var i = E(),
        a = i.elem.find(".layim-chat-main"),
        e = a.find("ul");
        if (e.find("li").length >= 50) {
            var t = e.find("li").eq(0);
            e.prev().hasClass("layim-chat-system") || e.before('<div class="layim-chat-system"><span layim-event="chatLog">查看更多记录</span></div>'),
            t.remove()
        }
        a.scrollTop(a[0].scrollHeight),
        a.find("ul li:last").find("img").load(function() {
            a.scrollTop(a[0].scrollHeight)
        })
    },
    J = function() {
        var i = E(),
        a = i.textarea;
        a.focus(),
        a.off("keydown").on("keydown",
        function(i) {
            var e = layui.data("layim")[k.mine.id] || {},
            t = i.keyCode;
            if ("Ctrl+Enter" === e.sendHotKey) return void(i.ctrlKey && 13 === t && M());
            if (13 === t) {
                if (i.ctrlKey) return a.val(a.val() + "\n");
                if (i.shiftKey) return;
                i.preventDefault(),
                M()
            }
        })
    },
    _ = function() {
        var i = ["[微笑]", "[嘻嘻]", "[哈哈]", "[可爱]", "[可怜]", "[挖鼻]", "[吃惊]", "[害羞]", "[挤眼]", "[闭嘴]", "[鄙视]", "[爱你]", "[泪]", "[偷笑]", "[亲亲]", "[生病]", "[太开心]", "[白眼]", "[右哼哼]", "[左哼哼]", "[嘘]", "[衰]", "[委屈]", "[吐]", "[哈欠]", "[抱抱]", "[怒]", "[疑问]", "[馋嘴]", "[拜拜]", "[思考]", "[汗]", "[困]", "[睡]", "[钱]", "[失望]", "[酷]", "[色]", "[哼]", "[鼓掌]", "[晕]", "[悲伤]", "[抓狂]", "[黑线]", "[阴险]", "[怒骂]", "[互粉]", "[心]", "[伤心]", "[猪头]", "[熊猫]", "[兔子]", "[ok]", "[耶]", "[good]", "[NO]", "[赞]", "[来]", "[弱]", "[草泥马]", "[神马]", "[囧]", "[浮云]", "[给力]", "[围观]", "[威武]", "[奥特曼]", "[礼物]", "[钟]", "[话筒]", "[蜡烛]", "[蛋糕]"],
        a = {};
        return layui.each(i,
        function(i, e) {
            a[e] = layui.cache.dir + "images/face/" + i + ".gif"
        }),
        a
    } (),
    K = function(i) {
        i = i || window.event,
        i.stopPropagation ? i.stopPropagation() : i.cancelBubble = !0
    },
    R = function(i, a) {
        var e, t = i.value;
        i.focus(),
        document.selection ? (e = document.selection.createRange(), document.selection.empty(), e.text = a) : (e = [t.substring(0, i.selectionStart), a, t.substr(i.selectionEnd)], i.focus(), i.value = e.join(""))
    },
    B = "layui-anim-up",
    F = {
        status: function(i, a) {
            var t = function() {
                i.next().hide().removeClass(B)
            },
            l = i.attr("lay-type");
            if ("show" === l) K(a),
            i.next().show().addClass(B),
            e(document).off("click", t).on("click", t);
            else {
                var n = i.parent().prev();
                i.addClass(s).siblings().removeClass(s),
                n.html(i.find("cite").html()),
                n.removeClass("layim-status-" + ("online" === l ? "hide": "online")).addClass("layim-status-" + l),
                layui.each(o.online,
                function(i, a) {
                    a && a(l)
                })
            }
        },
        tab: function(i) {
            var a, e = ".layim-tab-content",
            t = m.find(".layui-layim-tab>li");
            "number" == typeof i ? (a = i, i = t.eq(a)) : a = i.index(),
            a > 2 ? t.removeClass(s) : (F.tab.index = a, i.addClass(s).siblings().removeClass(s)),
            m.find(e).eq(a).addClass(n).siblings(e).removeClass(n)
        },
        spread: function(i) {
            var a = i.attr("lay-type"),
            e = "true" === a ? "false": "true",
            t = layui.data("layim")[k.mine.id] || {};
            i.next()["true" === a ? "removeClass": "addClass"](n),
            t["spread" + i.parent().index()] = e,
            layui.data("layim", {
                key: k.mine.id,
                value: t
            }),
            i.attr("lay-type", e),
            i.find(".layui-icon").html("true" === e ? "&#xe61a;": "&#xe602;")
        },
        search: function(i) {
            var a = m.find(".layui-layim-search"),
            e = m.find("#layui-layim-search"),
            t = a.find("input"),
            l = function(i) {
                var a = t.val().replace(/\s/);
                if ("" === a) F.tab(0 | F.tab.index);
                else {
                    for (var l = [], n = k.friend || [], s = k.group || [], o = "", c = 0; c < n.length; c++) for (var r = 0; r < (n[c].list || []).length; r++) - 1 !== n[c].list[r].username.indexOf(a) && (n[c].list[r].type = "friend", n[c].list[r].index = c, n[c].list[r].list = r, l.push(n[c].list[r]));
                    for (var d = 0; d < s.length; d++) - 1 !== s[d].groupname.indexOf(a) && (s[d].type = "group", s[d].index = d, s[d].list = d, l.push(s[d]));
                    if (l.length > 0) for (var u = 0; u < l.length; u++) o += '<li layim-event="chat" data-type="' + l[u].type + '" data-index="' + l[u].index + '" data-list="' + l[u].list + '"><img src="' + l[u].avatar + '"><span>' + (l[u].username || l[u].groupname || "佚名") + "</span><p>" + (l[u].remark || l[u].sign || "") + "</p></li>";
                    else o = '<li class="layim-null">无搜索结果</li>';
                    e.html(o),
                    F.tab(3)
                }
            }; ! k.base.isfriend && k.base.isgroup ? F.tab.index = 1 : k.base.isfriend || k.base.isgroup || (F.tab.index = 2),
            a.show(),
            t.focus(),
            t.off("keyup", l).on("keyup", l)
        },
        closeSearch: function(i) {
            i.parent().hide(),
            F.tab(0 | F.tab.index)
        },
        skin: function() {
            t.open({
                type: 1,
                title: "换肤",
                shade: !1,
                area: "300px",
                skin: "layui-box layui-layer-border",
                id: "layui-layim-skin",
                zIndex: 66666666,
                content: l(u).render({
                    skin: k.base.skin
                })
            })
        },
        find: function() {
            t.open({
                type: 2,
                title: "查找",
                shade: !1,
                area: ["1000px", "520px"],
                skin: "layui-box layui-layer-border",
                id: "layui-layim-find",
                content: k.base.find
            })
        },
        about: function() {
            t.alert("版本： " + a + '<br>版权所有：<a href="http://layim.layui.com" target="_blank">layim.layui.com</a>', {
                title: "关于 LayIM",
                shade: !1
            })
        },
        setSkin: function(i) {
            var a = i.attr("src"),
            e = layui.data("layim")[k.mine.id] || {};
            e.skin = a,
            a || delete e.skin,
            layui.data("layim", {
                key: k.mine.id,
                value: e
            });
            try {
                m.css({
                    "background-image": a ? "url(" + a + ")": "none"
                }),
                p.css({
                    "background-image": a ? "url(" + a + ")": "none"
                })
            } catch(t) {}
        },
        chat: function(i) {
            var a = layui.data("layim")[k.mine.id] || {},
            e = i.data("type"),
            t = i.data("index"),
            l = i.attr("data-list") || i.index(),
            n = {};
            "friend" === e ? n = k[e][t].list[l] : "group" === e ? n = k[e][l] : "history" === e && (n = (a.history || {})[t] || {}),
            n.name = n.name || n.username || n.groupname,
            "history" !== e && (n.type = e),
            T(n)
        },
        tabChat: function(i) {
            I(i)
        },
        closeChat: function(i) {
            I(i.parent(), 1)
        },
        closeThisChat: function() {
            I(null, 1)
        },
        groupMembers: function(i, a) {
            var l = i.find(".layui-icon"),
            n = function() {
                l.html("&#xe61a;"),
                i.data("down", null),
                t.close(F.groupMembers.index)
            },
            s = function(i) {
                K(i)
            };
            i.data("down") ? n() : (l.html("&#xe619;"), i.data("down", !0), F.groupMembers.index = t.tips('<ul class="layim-members-list"></ul>', i, {
                tips: 3,
                time: 0,
                shift: 5,
                fix: !0,
                skin: "layui-box layui-layim-members",
                success: function(a) {
                    var t = k.base.members || {},
                    l = E(),
                    n = "";
                    t.data = e.extend(t.data, {
                        id: l.data.id
                    }),
                    x(t,
                    function(e) {
                        layui.each(e.list,
                        function(i, a) {
                            n += '<li><a><img src="' + a.avatar + '"></a><p>' + a.username + "</p></li>"
                        }),
                        a.find(".layim-members-list").html(n),
                        layui.each(o.members,
                        function(i, a) {
                            a && a(e)
                        }),
                        i.find(".layim-chat-members").html((e.list || []).length + "人")
                    }),
                    a.on("mousedown",
                    function(i) {
                        K(i)
                    })
                }
            }), e(document).off("mousedown", n).on("mousedown", n), e(window).off("resize", n).on("resize", n), i.off("mousedown", s).on("mousedown", s))
        },
        send: function() {
            M()
        },
        setSend: function(i, a) {
            var t = i.siblings(".layim-menu-box"),
            l = function() {
                t.hide().removeClass(B)
            },
            n = i.attr("lay-type");
            if ("show" === n) K(a),
            t.show().addClass(B),
            e(document).off("click", l).on("click", l);
            else {
                i.addClass(s).siblings().removeClass(s);
                var o = layui.data("layim")[k.mine.id] || {};
                o.sendHotKey = n,
                layui.data("layim", {
                    key: k.mine.id,
                    value: o
                })
            }
        },
        face: function(i, a) {
            var l = "",
            n = E(),
            s = function() {
                t.close(F.face.index)
            };
            for (var o in _) l += '<li title="' + o + '"><img src="' + _[o] + '"></li>';
            l = '<ul class="layui-clear layim-face-list">' + l + "</ul>",
            F.face.index = t.tips(l, i, {
                tips: 1,
                time: 0,
                fix: !0,
                skin: "layui-box layui-layim-face",
                success: function(i) {
                    i.find(".layim-face-list>li").on("mousedown",
                    function(i) {
                        K(i)
                    }).on("click",
                    function() {
                        R(n.textarea[0], "face" + this.title + " "),
                        t.close(F.face.index)
                    })
                }
            }),
            e(document).off("mousedown", s).on("mousedown", s),
            e(window).off("resize", s).on("resize", s),
            K(a)
        },
        image: function(i) {
            var a = i.data("type") || "images",
            e = {
                images: "uploadImage",
                file: "uploadFile"
            },
            l = E(),
            n = k.base[e[a]] || {};
            layui.upload({
                url: n.url || "",
                method: n.type,
                file: i.find("input")[0],
                unwrap: !0,
                check: a,
                success: function(i) {
                    try {
                        i = JSON.parse(i)
                    } catch(e) {
                        return i = {},
                        t.msg("请对上传接口返回JSON字符")
                    }
                    0 == i.code ? (i.data = i.data || {},
                    "images" === a ? R(l.textarea[0], "img[" + (i.data.src || "") + "]") : "file" === a && R(l.textarea[0], "file(" + (i.data.src || "") + ")[" + (i.data.name || "下载文件") + "]")) : t.msg(i.msg || "上传失败")
                }
            })
        },
        chatLog: function(i) {
            var a = E();
            return k.base.chatLog ? (t.close(F.chatLog.index), F.chatLog.index = t.open({
                type: 2,
                maxmin: !0,
                title: "与 " + a.data.name + " 的聊天记录",
                area: ["450px", "100%"],
                shade: !1,
                offset: "rb",
                skin: "layui-box",
                shift: 2,
                id: "layui-layim-chatlog",
                content: k.base.chatLog + "?id=" + a.data.id + "&type=" + a.data.type
            })) : t.msg("未开启更多聊天记录")
        },
        menuHistory: function(i, a) {
            var l = layui.data("layim")[k.mine.id] || {},
            n = i.parent(),
            s = i.data("type"),
            o = m.find(".layim-list-history"),
            c = '<li class="layim-null">暂无历史会话</li>';
            if ("one" === s) {
                var r = l.history;
                delete r[n.data("index")],
                l.history = r,
                layui.data("layim", {
                    key: k.mine.id,
                    value: l
                }),
                e("#" + n.data("id")).remove(),
                0 === o.find("li").length && o.html(c)
            } else "all" === s && (delete l.history, layui.data("layim", {
                key: k.mine.id,
                value: l
            }), o.html(c));
            t.closeAll("tips")
        }
    };
    i("layim", new c)
}).addcss("pc/layim/layim.css?v=2.083", "skinlayimcss");